package com.jiangtour.controller;

import com.jiangtour.config.ProjectUrlConfig;
import com.jiangtour.constant.CookieConstant;
import com.jiangtour.constant.RedisConstant;
import com.jiangtour.enums.ResultEnum;
import com.jiangtour.exception.SiteException;
import com.jiangtour.service.SiteInfoService;
import com.jiangtour.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @Title: SiteUserController
 * @ProjectName site
 * @Description:
 * @date 2018/12/26/02617:49
 */

@Controller
@RequestMapping("/user")
public class SiteUserController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProjectUrlConfig projectUrlConfig;
    @Autowired
    private SiteInfoService siteInfoService;

    @GetMapping("/login")
    public ModelAndView login(@RequestParam("account") String account,
                              @RequestParam("password") String password,
                              HttpServletResponse response,
                              Map<String, Object> map) {
        //1.验证账号
        try {
            siteInfoService.login(account,password);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/user/index");
            return new ModelAndView("common/error", map);
        }

        String token = UUID.randomUUID().toString();
        Integer expire = RedisConstant.EXPIRE;
        //2.设置token到redis
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),account,expire, TimeUnit.SECONDS);

        //3.设置token到cookie
        CookieUtil.set(response,CookieConstant.TOKEN,token,CookieConstant.EXPIRE);

        if(account.equals("admin")){
            return new ModelAndView("redirect:" + projectUrlConfig.getSite() + "/admin/school/list");
        }
        return new ModelAndView("redirect:" + projectUrlConfig.getSite() + "/order/list?type=1");

    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,HttpServletResponse response,Map<String, Object> map){
        //1. 从cookie里查询
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);

        if (cookie != null) {
            //2. 清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));

            //3. 清除cookie
            CookieUtil.set(response, CookieConstant.TOKEN, null, 0);
        }

        map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url", "/order/list");
        return new ModelAndView("common/success", map);

    }
    @GetMapping("/index")
    public String index(){
        return "redirect:/commonModule/index.html";

    }

}
